
library(tidyverse)
library(stargazer)
library(sandwich)
library(AER)


# Tables 2 and 3 completed by hand
# Table 6 manually combines files CA18_subgroup.htm, OR18_subgroup.htm, OR18_subgroup.htm 
# as produced in replication files

# Functions to present robust standard errors

gaze.coeft <- function(x, col="Std. Error"){
  stopifnot(is.list(x))
  out <- lapply(x, function(y){
    y[ , col]
  })
  return(out)
}

gaze.lines.ivreg.diagn <- function(x, col="statistic", row=1:3, digits=1){
  stopifnot(is.list(x))
  out <- lapply(x, function(y){
    stopifnot(class(y)=="summary.ivreg")
    y$diagnostics[row, col, drop=FALSE]
  })
  out <- as.list(data.frame(t(as.data.frame(out)), check.names = FALSE))
  for(i in 1:length(out)){
    out[[i]] <- c(names(out)[i], round(out[[i]], digits=digits))
  }
  return(out)
}



##### Table 1

CA_logit_doydiff <- readRDS("./California/Object Files/CA_logit_doydiff.rds")
logit_doydiffOR2016 <- readRDS("./Oregon/Object Files 2016/logit_doydiffOR2016.rds")

cov1_CAlogit<- vcovHC(CA_logit_doydiff, type = "HC0")
robust_se_CAlogit <- sqrt(diag(cov1_CAlogit))

cov1_ORlogit<- vcovHC(logit_doydiffOR2016, type = "HC0")
robust_se_ORlogit <- sqrt(diag(cov1_ORlogit))

stargazer(logit_doydiffOR2016, CA_logit_doydiff,
          header = FALSE, 
          type = "html",
          digits = 3, 
#         se = list(robust_se_ORlogit, robust_se_CAlogit),
          column.labels = c("OR 2016", "CA 2018"),
          dep.var.labels = c("Automatic Voter Registration", "Constant"),
          dep.var.caption = "Dependent Variable: Registration Date Proximate to Birth Date",
          out="figures/TABLE1.htm")

##### Figure 2

CA_equiv <- readRDS("./California/Object Files/CA_equiv.rds")
OR18_equiv <- readRDS("./Oregon/Object Files 2018/OR18_equiv.rds")
OR16_equiv <- readRDS("./Oregon/Object Files 2016/OR16_equiv.rds")

equivalence_tests<-bind_rows(OR18_equiv, OR16_equiv, CA_equiv)

ggplot(equivalence_tests, aes(x = estimate, y = name)) +
  labs(x = "Equivalence range", y = "") +
  geom_segment(aes(x = lower, xend = upper, yend = name),
               size = 4, color = "grey90") +
  geom_point() +
  xlim(0.80, 1.2) +
  theme_light()+
  facet_grid(full ~ year)

ggsave(filename = "figures/Figure2.png")

########## Table 5

load("./California/Object Files/CA iv main robust.RData")
load("./Oregon/Object Files 2016/OR 16 iv main robust.RData")
load("./Oregon/Object Files 2018/OR 18 iv main robust.RData")

stargazer(iv1OR16, ivnewOR16, iv1OR18, ivnewOR18, iv1, ivnew, type = "html", 
          dep.var.labels =NULL, column.labels= c("OR 2016 - All", "OR 2016 - New", "OR 2018 - All", "OR 2018 - New", "CA 2018 - All", "CA 2018 - New"),
          se = gaze.coeft(list(iv1OR16.rob, ivnewOR16.rob, iv1OR18.rob, ivnewOR18.rob, iv1.rob, ivnew.rob)), df=FALSE, omit.stat=c("adj.rsq","ser"),
          add.lines = gaze.lines.ivreg.diagn(list(iv1OR16.sum, ivnewOR16.sum, iv1OR18.sum, ivnewOR18.sum, iv1.sum, ivnew.sum), row=1), out="figures/TABLE5.htm")



####### Table 7

load("./California/Object Files/CA iv sensitivity.RData")
load("./Oregon/Object Files 2016/OR iv sensitivity.RData")


stargazer(ORiv_any_vote_new, CAiv_any_vote_new,  type = "html", 
          dep.var.labels =NULL, column.labels= c("OR 2016", "CA 2018"),
          se = gaze.coeft(list(ORiv_any_vote_new.rob, CAiv_any_vote_new.rob)), df=FALSE, omit.stat=c("adj.rsq","ser"),
          add.lines = gaze.lines.ivreg.diagn(list(ORiv_any_vote_new.sum, CAiv_any_vote_new.sum), row=1), out="figures/TABLE7.htm")

